<?php

include_once(dirname(__FILE__) . "/_init.php");


/*
 * Whiteboard
 * Code that relating to elgg-whiteboard
 * Based on tidypics plugin
 */

global $CONFIG;

function greengar_init() {
    global $CONFIG;

    //elgg_extend_view('css', 'whiteboard/css');
    //elgg_extend_view('metatags', 'whiteboard/js');
    
    // Override so that pages with 'photos' page handler will use our handler instead of tidypics'
    register_page_handler(WHITEBOARD_TP_PAGE_HANDLER, 'greengarwhiteboardgallery_page_handler');
    //register_page_handler('dashboard', 'greengar_dashboard_page_handler');

    // Trivial trick to register our layout with Elgg engine
    //$CONFIG->views->locations['default']['canvas/layouts/wb_default'];
}

/*
 * Page handler. Handle both tydypics' pages & whiteboard's ones
 */

function greengarwhiteboardgallery_page_handler($page) {
    global $CONFIG;

    switch ($page[0]) {

        /*
         * Whiteboard's new pages
         */
//        case 'report':
//            if (isset($page[1]))
//                set_input('guid', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/pages/report.php');
//            break;
//        case 'search':
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/pages/search.php');
//            break;
//
//        case 'thismonthhighest':
//            if (isset($page[1]))
//                set_input('owner_guid', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/pages/lists/thismonthhighest.php');
//            break;
//
//        case 'mostrecentdrawings':
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/pages/lists/mostrecentdrawings.php');
//            break;
//
//        case 'ownedwhiteboard':
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/ownedalbums.php");
//            break;
//
//        case 'worldwhiteboard':
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/world.php");
//            break;

        /*
         * Tidypics' pages that needed to be changed somewhere
         */
//        case "album": //view an album individually
//            if (isset($page[1]))
//                set_input('guid', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/viewalbum.php");
//            break;

//        case "download": // download an image
//            if (isset($page[1]))
//                set_input('file_guid', $page[1]);
//            if (isset($page[2]))
//                set_input('type', $page[2]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/download.php");
//            break;

        case "thumbnail": // tidypics thumbnail
            if (isset($page[1]))
                set_input('file_guid', $page[1]);
            if (isset($page[2]))
                set_input('size', $page[2]);
            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/thumbnail.php");
            break;

//        case "view": //view an image individually
//            if (isset($page[1]))
//                set_input('guid', $page[1]);
//            include($CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . "/pages/viewimage.php");
//            break;

        /*
         * Tidypics' original pages
         */
//        case "owned":  //view list of albums owned by container
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/ownedalbums.php");
//            break;
//
//        case "new":  //create new album
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/newalbum.php");
//            break;
//
//        case "upload": //upload images to album
//            if (isset($page[1]))
//                set_input('container_guid', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/upload.php");
//            break;
//
//        case "edit": //edit image or album
//            if (isset($page[1]))
//                set_input('guid', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/edit.php");
//            break;
//
//        case "friends": // albums of friends
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/friends.php");
//            break;
//
//        case "world": // all site albums
//            include($CONFIG->pluginspath . "tidypics/pages/world.php");
//            break;
//
//        case "tagged": // all photos tagged with user 
//            if (isset($page[1]))
//                set_input('guid', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/tagged.php");
//            break;
//
//        case "mostviewed": // images with the most views
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/lists/mostviewedimages.php");
//            break;
//
//        case "mostrecent": // images uploaded most recently
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/lists/mostrecentimages.php");
//            break;
//
//        case "recentlyviewed": // images most recently viewed
//            include($CONFIG->pluginspath . "tidypics/pages/lists/recentlyviewed.php");
//            break;
//
//        case "recentlycommented": // images with the most recent comments
//            include($CONFIG->pluginspath . "tidypics/pages/lists/recentlycommented.php");
//            break;
//
//        case "highestrated": // images with the highest average rating
//            include($CONFIG->pluginspath . "tidypics/pages/lists/highestrated.php");
//            break;
//
//        case "flickr":
//            if (isset($page[1]))
//                set_input('username', $page[1]);
//            include($CONFIG->pluginspath . "tidypics/pages/flickr/setup.php");
//            break;
    }

    return TRUE;
}

function greengar_dashboard_page_handler($page)
{
    global $CONFIG;
    include($CONFIG->pluginspath.WHITEBOARD_PLUGIN_DIR.'/pages/dashboard.php');
}

/*
 * Register actions
 */
//register_action(WHITEBOARD_ACTION_HANDLER . '/rotate', false, $CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/actions/rotate.php');
//register_action(WHITEBOARD_ACTION_HANDLER . '/hidedrawing', true, $CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/actions/hidedrawing.php');
//register_action(WHITEBOARD_ACTION_HANDLER . '/unhidedrawing', true, $CONFIG->pluginspath . WHITEBOARD_PLUGIN_DIR . '/actions/unhidedrawing.php');

/*
 * Widgets
 */
//add_widget_type('whiteboard_menu', 'Whiteboard Gallery', 'Whiteboard gallery options', 'all');
//add_widget_type('whiteboard_my_latest',
//        '<a href="'.$CONFIG->wwwroot.'pg/'.WHITEBOARD_TP_PAGE_HANDLER.'/mostrecentdrawings/'.get_loggedin_user()->username.'">Your Latest Whiteboard Drawings</a>',
//        'Your most recent drawings', 'all');
//add_widget_type('whiteboard_all_latest',
//        '<a href="'.$CONFIG->wwwroot.'pg/'.WHITEBOARD_TP_PAGE_HANDLER.'/mostrecentdrawings/">Latest Whiteboard Drawings</a>',
//        'All site\'s most recent drawings', 'all');
//add_widget_type('whiteboard_my_highestrated_thismonth', 'My Highest Rated Drawings This Month', '', 'all');
//add_widget_type('whiteboard_all_highestrated_thismonth',
//        '<a href="'.$CONFIG->wwwroot.'pg/'.WHITEBOARD_TP_PAGE_HANDLER.'/thismonthhighest/">All Highest Rated Drawings This Month</a>',
//        '', 'all');

/*
 * Neccessary plugin hooks
 */
//register_plugin_hook('index', 'system', 'greengar_custom_index');
//register_plugin_hook('wb_ratebox', WHITEBOARD_DRAWING_SUBTYPE, 'greengar_ratebox');

/*
 * Adding tab menu using the same mechanism of elgg submenu
 * See also greengar_get_tabmenu()
 */
//register_elgg_event_handler('pagesetup', 'system', 'greengarwhiteboardgallery_submenus');

function greengarwhiteboardgallery_submenus() {
    global $CONFIG;

    if (get_context() == WHITEBOARD_TP_PAGE_HANDLER) {
//        greengar_add_tabmenu_item('My highest rated', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/thismonthhighest/' . get_loggedin_userid());
//        greengar_add_tabmenu_item('All highest rated', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/thismonthhighest/');
//        greengar_add_tabmenu_item('My most recent', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/mostrecentdrawings/' . get_loggedin_user()->username);
//        greengar_add_tabmenu_item('All most recent', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/mostrecentdrawings/');
//        greengar_add_tabmenu_item('Your albums', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/ownedwhiteboard/' . get_loggedin_user()->username);
//        greengar_add_tabmenu_item('All whiteboard albums', $CONFIG->wwwroot . 'pg/' . WHITEBOARD_TP_PAGE_HANDLER . '/worldwhiteboard/');
    }
}

/*
 * End of whiteboard
 */


// Initialise plugin
register_elgg_event_handler('init', 'system', 'greengar_init');

register_page_handler('greengar', 'greengar_page_handler');

register_elgg_event_handler('pagesetup', 'system', 'greengar_opinions_modmenu');

//register_action("greengar/opinions/report", false, $CONFIG->pluginspath. "greengar/actions/opinions/report.php");

if ( ! function_exists('curPageURL') ) {
	function curPageURL() {
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
}

function greengar_page_handler($page) {

    $bypass_oauth2 = array(
		'auth_by_email',
        'token',
        'addclient',
        'register',
        'facebook_login',
        'gamecenter_login',
        'gamecenter_register',
        'twitter_login',
        'twitter_register',
        'reset_password',
        'confirm_email',
        'confirm_reset_password',
        'thumbnail',
        'web_message',
        'report_abused',
        'echo_api',
        'upload',
		'claimurl',
        'get_drawings',
        'get_like',
		'get_comment',
        'auto_suggest_interests',
        'intro',
        'port_drawings',
        'send_mail',
        
        'fb_login',
        'gallery',
        'leaderboards'
        //'upload',
        //'create_game',
        //'update_game',
        //'discard_game',
        //'publish_game',
        ,'list_games2'
    );

    global $CONFIG;

    if (array_search($page[1], $bypass_oauth2) !== FALSE) {
//        echo "bypass";
    } else {
//        echo "trapped";
        oauth2_page_guardian();
    }
    //echo $CONFIG->pluginspath . 'greengar/' . $page[0] . '/pages/' . $page[1] . '.php';
	$timer1 = explode(' ', microtime());
    $timer1 = $timer1[1] + $timer1[0];
	
    include($CONFIG->pluginspath . 'greengar/' . $page[0] . '/pages/' . $page[1] . '.php');
	
	$timer2 = explode(' ', microtime());
    $timer2 = $timer2[1] + $timer2[0];
	
	$doneTime = round($timer2 - $timer1, 4);
    $myFile = "logs-except-oauth2-" . date("j-n-Y") . ".txt";
    $fh = fopen($myFile, 'a');
    $stringData = date('H:i:s') . " " . curPageURL() . " " . $doneTime . " " . " \n";
    fwrite($fh, $stringData);    
    fclose($fh);
    //logout();
}

/**
 * Sets up tidypics admin menu. Triggered on pagesetup.
 */
function greengar_opinions_modmenu() {
    global $CONFIG;
    if (get_context() == 'admin' && isadminloggedin()) {
        add_submenu_item(elgg_echo('Opinions moderation'), $CONFIG->url . "mod/greengar/opinions/moderation/pages/admin.php");
    }
}

?>